import pyautogui
import pygetwindow
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import time

from GUI.AutoWorker import AutoWorker
from GUI.MainGUI import Ui_Dialog
from Moniter.MainMoniter import Moniter


class MyMainWindow(QDialog, Ui_Dialog):
    def __init__(self):
        super().__init__()

        self.setupUi(self)
        self.setWindowFlags(Qt.WindowStaysOnTopHint)  # 设置窗口在最前面

        # 初始化变量
        self.button_status = 1
        self._translate = QCoreApplication.translate    # 改变按钮上的文字

        # pyautogui.alert('正在初始化', '提示', '我会等')
        self.init_work()
        self.bind_event()

    def init_work(self):
        # 初始化各种线程
        self.m = Moniter()
        print('**GUIController** before init autoworker')
        self.auto_worker = AutoWorker()
        print('**GUIController** has init all workers')

    def bind_event(self):
        # 绑定各种事件
        self.pushButton.clicked.connect(self.buy_event)       # 直接买入
        self.pushButton_2.clicked.connect(self.sell_event)    # 委托买入
        self.pushButton_3.clicked.connect(self.sell_event)    # 直接卖出
        self.pushButton_4.clicked.connect(self.sell_event)    # 委托卖出
        # self.auto_worker.sinOut.connect(self.end_trade)     # 交易结束时的资金结算
        print('**GUIController** has bind all events')

    def sell_event(self):
        if self._do_check():
            stock = self.get_stock()
            if stock is None:
                pyautogui.alert(title='警告', text='未收录这只股票的数据，请联系开发者解决此问题', button='明白')
            else:
                self.do_sell(stock)

    def buy_event(self, event):
        if self._do_check():
            stock = self.get_stock()
            if stock is None:
                pyautogui.alert(title='警告', text='未收录这只股票的数据，请联系开发者解决此问题', button='明白')
            else:
                self.do_buy(stock)

    def get_stock(self):
        print('**GUIController** get stock')
        new, stock = self.m.main_new
        print(stock)
        return stock

    def do_sell(self, stock):
        self.auto_worker.start_sell(stock=stock, value=self.spinBox.value())

    def do_buy(self, stock):
        self.auto_worker.start_buy(stock=stock, value=self.spinBox.value())
        # self.auto_worker = AutoWorker()
        # print('**GUIController** get new stock', stock_info)
        # text = '别动鼠标了！'
        # pyautogui.alert(title='警告', text=text, button='我再试试看')
        # # TODO: 这里可以加一个气泡弹窗提示，QToolTip，书P157；提示的目的是让用户别动鼠标了！
        # self.auto_gui(None, stock_info=stock_info)

    def _do_check(self):
        """ 检测预警窗口和交易软件 """
        # TODO:但凡弹窗就卡，艹；可以用QDiolog,书P191
        wins = pygetwindow.getWindowsWithTitle('网上股票交易系统5.')    # 检测网上交易系统窗口
        if len(wins) == 0:
            wins = pygetwindow.getWindowsWithTitle('记事本')
        print('get 交易', wins)
        if len(wins) != 1:
            text = '未打开东海证券网上交易系统或打开了其他软件，请关闭命名相似的软件，再重试'
            pyautogui.alert(title='警告', text=text, button='我再试试看')
            return False
        else:
            return True and self.m.check()

